//
//  NSString+limit.m
//  NSString
//
//  Created by apple on 15/7/30.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "NSString+limit.h"

@implementation NSString (limit)

- (NSString *)limitInTextFieldLength:(UITextField *)textField length:(NSInteger)length {
    NSString *toBeString = textField.text;
    NSArray *currentar = [UITextInputMode activeInputModes];
    UITextInputMode *current = [currentar firstObject];
    
    //下面的方法是iOS7被废弃的，注释
    //    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    
    if ([current.primaryLanguage isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > length) {
                textField.text = [toBeString substringToIndex:length];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > length) {
            textField.text = [toBeString substringToIndex:length];
        }
    }
    return textField.text;
}

- (NSString *)limitInTextViewLength:(UITextView *)textView length:(NSInteger)length {
    NSString *toBeString = textView.text;
    NSArray *currentar = [UITextInputMode activeInputModes];
    UITextInputMode *current = [currentar firstObject];
    
    //下面的方法是iOS7被废弃的，注释
    //    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    
    if ([current.primaryLanguage isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > length) {
                textView.text = [toBeString substringToIndex:length];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > length) {
            textView.text = [toBeString substringToIndex:length];
        }
    }
    return textView.text;
}


- (BOOL)limitOnlyLetters {
    NSCharacterSet *letterCharacterset = [[NSCharacterSet letterCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:letterCharacterset].location == NSNotFound);
}

- (BOOL)limitOnlyNumbers {
    NSCharacterSet *numSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    return ([self rangeOfCharacterFromSet:numSet].location == NSNotFound);
}

- (BOOL)limitOnlyAlphaNumeric {
    NSCharacterSet *numAndLetterCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
    return ([self rangeOfCharacterFromSet:numAndLetterCharSet].location == NSNotFound);
}

- (BOOL) validateMobile
{
    //^1[3|4|5|8|7]\d{9}$备用
    //^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^1[3|4|5|8|7]\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

- (BOOL) validatePassword
{
    //密码
    NSString *passwordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passwordRegex];
    return [passwordTest evaluateWithObject:self];
}

- (BOOL)validateAccount {
    NSString *regex = @"^[a-zA-Z0-9]{4,20}$";
    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [test evaluateWithObject:self];
}



- (NSString *)stringAddMoney {
    NSString *str = [NSString stringWithFormat:@"%@元",self];
    return str;
}

+ (NSString *)float4WithString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%.4f",[string floatValue]];
    return str;
}
+ (NSString *)float3WithString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%.3f",[string floatValue]];
    return str;
}
+ (NSString *)float2WithString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%.2f",[string floatValue]];
    return str;
}
+ (NSString *)float1WithString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%.1f",[string floatValue]];
    return str;
}
+ (NSString *)floatWithString:(NSString *)string {
    NSString *str = [NSString stringWithFormat:@"%.f",[string floatValue]];
    return str;
}

+ (NSString*) getIphoneUUID {
//    CFUUIDRef puuid = CFUUIDCreate( nil );
//    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
//    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
//    CFRelease(puuid);
//    CFRelease(uuidString);
//    NSLog(@"NSString+limit-uuid:%@",result);
    NSLog(@"%@",[[UIDevice currentDevice].identifierForVendor UUIDString]);
    return [[UIDevice currentDevice].identifierForVendor UUIDString];
}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

+ (NSString *)getIOSVersion
{
    return [[UIDevice currentDevice] systemVersion];
}

@end
